package com.qianniu.napi.admin.exception;

import com.qianniu.napi.admin.entity.Jinfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@CrossOrigin
@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler
    public Jinfo processException(Exception ex, HttpServletRequest request, HttpServletResponse response) {
        ex.printStackTrace();

        if (ex instanceof MissingServletRequestParameterException) {
            return new Jinfo(1, ex.getMessage());
        }
        if (ex instanceof NoFoundExcepiton) {
            LOGGER.error("=======" + ex.getMessage() + "=======");
            return new Jinfo(4, "无法找到相应数据！");

        }

        return new Jinfo(5, ex.getMessage());
    }
}
